package com.minisoft.pingan;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.http.HttpUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.*;

/**
 * @author JueSu
 * @description: TODO
 * @date 2023/12/10 12:23
 */
public class ExportBaoJiaUrl {
    public static void main(String[] args) {
        String outputFilePath = "D:\\temp/tuanchebaojia-"+ DateUtil.format(new Date(), DatePattern.PURE_DATETIME_PATTERN) +".xlsx";
        List<String> columnList = ListUtil.toList("车牌号","车架号","发动机号","座位/吨位","初次登记日期","车辆种类");


        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("Cookie","SESSION=MjdlMjlmZWEtOTgzMS00M2ZkLWEyODYtNDhjZGUwMjFiZmFk; us=stable_14; PA_GREY=EX-PANTANGMEI488; BIGipServerPOOL_PACLOUD_PRDR2019112805992=1243783639.33420.0000; PAMO_SESSION=f159397eb1e04ffdad48f7f3c5cd1e0d_v2; hm_sessionid=f159397eb1e04ffdad48f7f3c5cd1e0d_v2; routeopr=40611357cd965b27797134997cf8abfc; BIGipServerPOOL_PACLOUD_PRDR20230106232593=3783298007.5162.0000; v1LIHNTx4qTOO=5IO7g6AH5IjTxtheFTuZIg0bx0XGmRY07sHITFQJSfpcQgXx2Vq10dlSFHeC.AyPs5jyXZ_BgpisHdgm1sCdEvG; USER_REDIS_COOKIE=RVgtUEFOVEFOR01FSTQ4ODE3MDMzMjY1ODMyNDYxODk1OTM2NDRfMTcwMzMzMDk0MjY3MA%3D%3D; v1LIHNTx4qTOP=YGZUXRvFHdTvmPmpn.vGFluH3brXGMJI4_1SChOc4JrbiODIdRelFoA3v3n7wjD53vP.SIsN7V2vlAr022DJlqImSTb09sVRZJEBRGzDwFTSqCD222e3fAAVJZ1SKy6ik0jVS6VWsxDbVnqtkgzxt6R9RTRrkL8JPPCXbYMc1O.qLfSnxCQlNxCGWp2tiMgOvuqsx7n8pEKhpP1JPuP_dD0_l6V2y10CCWZfZ0etKkSO.I.5D.UNTRh.g2TKTFU4scBkbXZN94NKmT62M5oGXrqWojry7vTXJu1M1ahhMoo8xAlTo0lYzs.ywk_gJMy.vefcq9XnGqNwWFUnPf5tNeeIQ4Dy4Zre0ZgNbzxLpdQ");
        List<String> urls = FileUtil.readUtf8Lines(ExportBaoJiaUrl.class.getResource("/tuanchebaojiaUrls.txt"));

        List<List<Object>> rows = new ArrayList<>();
        for (int i = 0;i <= urls.size();i++) {
            Console.log("处理文件：{}/{}",i,urls.size());
            // Read JSON data from the input file
            String url = "https://icorepnbs.pingan.com.cn/icore_pnbs/do/app/workbench/queryVoucherDetailAndBaseData?fhiUFUxo=X_JdfGlqEhz4_xje_QnVksQrbai2Ob.n0jFWcd0rH3EeJGcYGqjmGxrKn1mx_MuRPPG_5g3dD3vbNRih55l4UlSeb9jbzYdT.lR.UnJDX9574Jy1KLuqIO4tuz0TsrS7a4AZQoBpkBe22EEM3Y_9IkxZLDOPCPVyKdtpFk1BorzTWyTFdjxyui9CMdirkLxt_ST2Ne.8zR1BV5Fpo0.5pM6SgR2DgjMnO8rWJfzHvUkqsMiq1DEHH_0";
            String jsonData = HttpUtil.createPost(url).headerMap(headerMap, true).execute().body();
            // Parse JSON data to get rows and column_list
            JSONObject jsonObject = JSON.parseObject(jsonData);
            JSONObject data = jsonObject.getJSONObject("quotationResultDTOJson");
            JSONObject vehicleTarget = data.getJSONArray("voucherDetailList").getJSONObject(0).getJSONObject("voucher").getJSONObject("vehicleTarget");

            List<Object> row = new ArrayList<>();
            row.add(vehicleTarget.get("vehicleLicenceCode"));
            row.add(vehicleTarget.get("vehicleFrameNo"));
            row.add(vehicleTarget.get("engineNo"));
            row.add(vehicleTarget.get("vehicleSeats") + "/" + vehicleTarget.getString("wholeWeight"));
            row.add(vehicleTarget.getString("firstRegisterDate").split(" ")[0]);
            row.add(vehicleTarget.get("vehicleTypeName"));

            rows.add(row);
        }
        // Create Excel writer
        ExcelWriter writer = ExcelUtil.getWriter(outputFilePath);

        // Set the header row
        writer.writeHeadRow(columnList);

        // Write data rows
        writer.write(rows);

        // Close the writer and save the Excel file
        writer.close();

        System.out.println("done");
    }
}
